//
//  HWRegistTypeController.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/2/7.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit

class HWRegistTypeController: HWBaseViewController {
    var collectionView:UICollectionView!
    let collectionProtocol = HWRegistTypeProtocol()
    let httpManager = HWHttpManager()
    var type = HWMineNavType.registType
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        
        initFrame()
        
        loadData()
    }
}

extension HWRegistTypeController {
    private func initUI() -> Void {
        if type == .registType {
            navigationView.titleLabel.text = "注册方式"
        }else if type == .rechargeType {
            navigationView.titleLabel.text = "充值方式"
        }
        
        let flowL = UICollectionViewFlowLayout.init()
        flowL.minimumLineSpacing = 0
        flowL.minimumInteritemSpacing = 0
        
        view.backgroundColor = .white
        collectionProtocol.type = type
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowL)
        collectionView.delegate = collectionProtocol
        collectionView.dataSource = collectionProtocol
        collectionView.showsVerticalScrollIndicator = false
        collectionView.contentInsetAdjustmentBehavior = .never
        collectionView.backgroundColor = .colorWithHexString(hexString: "#F5F5F5")
        collectionView.register(HWRegistTypePieCell.self, forCellWithReuseIdentifier: "HWRegistTypePieCell")
        collectionView.register(HWRegistTypeCell.self, forCellWithReuseIdentifier: "HWRegistTypeCell")
        collectionView.register(HWHomeHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HWHomeHeader")
        view.addSubview(collectionView)
        
        view.bringSubviewToFront(navigationView)
    }
    
    private func initFrame() -> Void {
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(navigationView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }
}

extension HWRegistTypeController {
    func loadData() -> Void {
        if type == .registType {
            httpManager.getRegistTypeInfo() { infoModel in
                self.collectionView.mj_header?.endRefreshing()
                self.collectionView.mj_footer?.endRefreshing()
                self.collectionProtocol.model = infoModel
                self.collectionView.reloadData()
            } failure: { error in
                self.collectionView.mj_header?.endRefreshing()
                self.collectionView.mj_footer?.endRefreshing()
                self.view.showHUD(title: error)
            }
        }else if type == .rechargeType {
            httpManager.getRechargeTypeInfo() { infoModel in
                self.collectionView.mj_header?.endRefreshing()
                self.collectionView.mj_footer?.endRefreshing()
                self.collectionProtocol.rechargeTypeModel = infoModel
                self.collectionView.reloadData()
            } failure: { error in
                self.collectionView.mj_header?.endRefreshing()
                self.collectionView.mj_footer?.endRefreshing()
                self.view.showHUD(title: error)
            }
        }
    }
}
